/**
 * This class creates a bar chart graph from the preprocessed data file given as input with the two attrivutes
 */
package org.wmine.visualization;

import java.awt.Color;
import java.awt.GradientPaint;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.StringBufferInputStream;
import java.util.ArrayList;
import java.util.Iterator;

import javax.swing.JOptionPane;
import javax.xml.parsers.ParserConfigurationException;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.wmine.core.ProjectManager;
import org.wmine.data.Attribute;
import org.wmine.data.DataSet;
import org.wmine.data.InvalidCursorPositionException;
import org.wmine.data.TableResultSet;
import org.wmine.data.XMLDataReader;
import org.xml.sax.SAXException;

/**
 * @author Rajeev Kumar Thakur
 *
 */
public class DrawBarChartGraph implements IDrawMiningAlgorithmOutput {
	private String inputFileName, inputAttrib1,inputAttrib2;
	ArrayList<Double> yData;
	ArrayList<String> xData;
	DataSet dataSet;
	public DrawBarChartGraph(String preprocessInputFileName, String preprocessAttrib1, String preprocessAttrib2) {
		// TODO Auto-generated constructor stub
		inputFileName = preprocessInputFileName;
		inputAttrib1 = preprocessAttrib1;
		inputAttrib2 = preprocessAttrib2;
		xData = new ArrayList<String>();
		yData = new ArrayList<Double>();
		dataSet = new DataSet();
		try {
			dataSet.setDataReader(new XMLDataReader(new StringBufferInputStream(ProjectManager.getUniqueInstance().readFile(inputFileName))));
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/* (non-Javadoc)
	 * @see org.wmine.visualization.IDrawMiningAlgorithmOutput#drawGraph()
	 */
	public SetChartPanel drawGraph() throws ParserConfigurationException,
			SAXException, IOException {
		// TODO Auto-generated method stub
		JFreeChart chart;
		SetChartPanel setChart;
		TableResultSet resultSet = dataSet.getTableResultSet();
		resultSet.reset();
		if(checkAttributType())	{		
			while (resultSet.hasNext()) {
				try {
					xData.add(resultSet.getString(inputAttrib1));
					yData.add(Double.parseDouble(resultSet.getString(inputAttrib2)));
				} catch (NumberFormatException e) {
					// TODO Auto-generated catch block
					
					e.printStackTrace();
				} catch (InvalidCursorPositionException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
				}
			chart = setupChartProperties();
			setChart = new SetChartPanel(chart);
			}
			else {
				JOptionPane.showMessageDialog(null, "Select the Numerical data at Y axis data");
				setChart = null;
			}
			
			
			return setChart;
		
	}
	protected JFreeChart setupChartProperties() {
		JFreeChart chart;
		DefaultCategoryDataset dataset = createDataset();
		
		chart = createChart(dataset);
	
		return chart;
	}
	
	private JFreeChart createChart(DefaultCategoryDataset dataset2) {
		// TODO Auto-generated method stub
		JFreeChart chart = ChartFactory.createBarChart(
				"Bar Chart Graph for selected Attributes", // chart title
				inputAttrib1, // domain axis label
				inputAttrib2, // range axis label
				dataset2, // data
				PlotOrientation.VERTICAL,
				true, // include legend
				true, // tooltips?
				false // URLs?
				);
//				 NOW DO SOME OPTIONAL CUSTOMISATION OF THE CHART...
//				 set the background color for the chart...
				//chart.setBackgroundPaint(new Color(0xBBBBDD));
//				 get a reference to the plot for further customisation...
				CategoryPlot plot = chart.getCategoryPlot();
//				 set the range axis to display integers only...
				NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
				rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
//				 disable bar outlines...
				org.jfree.chart.renderer.category.BarRenderer renderer = (org.jfree.chart.renderer.category.BarRenderer) plot.getRenderer();
				renderer.setDrawBarOutline(false);
//				 set up gradient paints for series...
				GradientPaint gp0 = new GradientPaint(
				0.0f, 0.0f, Color.blue,
				0.0f, 0.0f, Color.lightGray
				);
				renderer.setSeriesPaint(0, gp0);
					 
//				 OPTIONAL CUSTOMISATION COMPLETED.
				return chart;		
	}

	private DefaultCategoryDataset createDataset() {
		// TODO Auto-generated method stub
		String series1 = "First";
		DefaultCategoryDataset data = new DefaultCategoryDataset();
		for (int i = 0; i < xData.size(); i++) {
			data.addValue(yData.get(i), series1, xData.get(i));
		}
		return data;
	}

	private boolean checkAttributType() {
		// TODO Auto-generated method stub
		boolean flag = true; 
		Iterator <Attribute> iterAttrib;
		iterAttrib = dataSet.getAttributeIterator();
		while (iterAttrib.hasNext()) {
			Attribute element = (Attribute) iterAttrib.next();
			String name = element.getAttributeName();
			String type = element.getAttributeType();
			if(name.equals(inputAttrib2)){
				if (type.equals("String")) {
					//JOptionPane.showMessageDialog(null, "Select the Numerical data as attribute");
					flag = false;
					break;
				}					
			}
		
		}
		return flag;
	}
}
/**
 * This class creates a bar chart graph from the preprocessed data file given as input with the two attrivutes
 */
package org.wmine.visualization;

import java.awt.Color;
import java.awt.GradientPaint;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.StringBufferInputStream;
import java.util.ArrayList;
import java.util.Iterator;

import javax.swing.JOptionPane;
import javax.xml.parsers.ParserConfigurationException;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.wmine.core.ProjectManager;
import org.wmine.data.Attribute;
import org.wmine.data.DataSet;
import org.wmine.data.InvalidCursorPositionException;
import org.wmine.data.TableResultSet;
import org.wmine.data.XMLDataReader;
import org.xml.sax.SAXException;

/**
 * @author Rajeev Kumar Thakur
 *
 */
public class DrawBarChartGraph implements IDrawMiningAlgorithmOutput {
	private String inputFileName, inputAttrib1,inputAttrib2;
	ArrayList<Double> yData;
	ArrayList<String> xData;
	DataSet dataSet;
	public DrawBarChartGraph(String preprocessInputFileName, String preprocessAttrib1, String preprocessAttrib2) {
		// TODO Auto-generated constructor stub
		inputFileName = preprocessInputFileName;
		inputAttrib1 = preprocessAttrib1;
		inputAttrib2 = preprocessAttrib2;
		xData = new ArrayList<String>();
		yData = new ArrayList<Double>();
		dataSet = new DataSet();
		try {
			dataSet.setDataReader(new XMLDataReader(new StringBufferInputStream(ProjectManager.getUniqueInstance().readFile(inputFileName))));
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/* (non-Javadoc)
	 * @see org.wmine.visualization.IDrawMiningAlgorithmOutput#drawGraph()
	 */
	public SetChartPanel drawGraph() throws ParserConfigurationException,
			SAXException, IOException {
		// TODO Auto-generated method stub
		JFreeChart chart;
		SetChartPanel setChart;
		TableResultSet resultSet = dataSet.getTableResultSet();
		resultSet.reset();
		if(checkAttributType())	{		
			while (resultSet.hasNext()) {
				try {
					xData.add(resultSet.getString(inputAttrib1));
					yData.add(Double.parseDouble(resultSet.getString(inputAttrib2)));
				} catch (NumberFormatException e) {
					// TODO Auto-generated catch block
					
					e.printStackTrace();
				} catch (InvalidCursorPositionException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
				}
			chart = setupChartProperties();
			setChart = new SetChartPanel(chart);
			}
			else {
				JOptionPane.showMessageDialog(null, "Select the Numerical data at Y axis data");
				setChart = null;
			}
			
			
			return setChart;
		
	}
	protected JFreeChart setupChartProperties() {
		JFreeChart chart;
		DefaultCategoryDataset dataset = createDataset();
		
		chart = createChart(dataset);
	
		return chart;
	}
	
	private JFreeChart createChart(DefaultCategoryDataset dataset2) {
		// TODO Auto-generated method stub
		JFreeChart chart = ChartFactory.createBarChart(
				"Bar Chart Graph for selected Attributes", // chart title
				inputAttrib1, // domain axis label
				inputAttrib2, // range axis label
				dataset2, // data
				PlotOrientation.VERTICAL,
				true, // include legend
				true, // tooltips?
				false // URLs?
				);
//				 NOW DO SOME OPTIONAL CUSTOMISATION OF THE CHART...
//				 set the background color for the chart...
				//chart.setBackgroundPaint(new Color(0xBBBBDD));
//				 get a reference to the plot for further customisation...
				CategoryPlot plot = chart.getCategoryPlot();
//				 set the range axis to display integers only...
				NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
				rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
//				 disable bar outlines...
				org.jfree.chart.renderer.category.BarRenderer renderer = (org.jfree.chart.renderer.category.BarRenderer) plot.getRenderer();
				renderer.setDrawBarOutline(false);
//				 set up gradient paints for series...
				GradientPaint gp0 = new GradientPaint(
				0.0f, 0.0f, Color.blue,
				0.0f, 0.0f, Color.lightGray
				);
				renderer.setSeriesPaint(0, gp0);
					 
//				 OPTIONAL CUSTOMISATION COMPLETED.
				return chart;		
	}

	private DefaultCategoryDataset createDataset() {
		// TODO Auto-generated method stub
		String series1 = "First";
		DefaultCategoryDataset data = new DefaultCategoryDataset();
		for (int i = 0; i < xData.size(); i++) {
			data.addValue(yData.get(i), series1, xData.get(i));
		}
		return data;
	}

	private boolean checkAttributType() {
		// TODO Auto-generated method stub
		boolean flag = true; 
		Iterator <Attribute> iterAttrib;
		iterAttrib = dataSet.getAttributeIterator();
		while (iterAttrib.hasNext()) {
			Attribute element = (Attribute) iterAttrib.next();
			String name = element.getAttributeName();
			String type = element.getAttributeType();
			if(name.equals(inputAttrib2)){
				if (type.equals("String")) {
					//JOptionPane.showMessageDialog(null, "Select the Numerical data as attribute");
					flag = false;
					break;
				}					
			}
		
		}
		return flag;
	}
}
